from PySide6.QtGui import QStandardItemModel, QStandardItem
from PySide6.QtWidgets import QTableWidgetItem
from pyecharts.charts import Bar
from pyecharts.faker import Faker
import pyecharts.options as opts
from ui.cardTabelAddDialog import Ui_cardTableAddDialog
from ui.goodsEditDialog import Ui_Dialog
from model.goodsModel import Goods

class Context:
    def __init__(self, mainWindow):
        self.mainWindow = mainWindow
        self.signalConnect()

    def signalConnect(self):
        self.mainWindow.leftListWidget.clicked.connect(self.switchStackView)
        self.mainWindow.chartsTabWidget.currentChanged.connect(self.switchCharts)
        self.mainWindow.chartsTabWidget.currentChanged.connect(self.switchCharts)
        self.mainWindow.chartsTabWidget.currentChanged.connect(self.switchCharts)

        self.mainWindow.cardTableAddBtn.clicked.connect(self.add)
        self.mainWindow.goodsAddBtn.clicked.connect(self.goodsAddBtnClicked)
        self.mainWindow.goodsSearchBtn.clicked.connect(self.goodsSearchBtnClicked)


    def goodsSearchBtnClicked(self):
        data = Goods().queryAll()
        table = self.mainWindow.goodsTableWidget
        table.setRowCount(len(data))
        index = -1
        for goods in data:
            index = index + 1
            item = QTableWidgetItem()
            item.setText(goods.code)
            table.setItem(index, 0, item)

            item = QTableWidgetItem()
            item.setText(goods.name)
            table.setItem(index, 1, item)
            print(index)

    def goodsAddBtnClicked(self):
        print("check")
        dialog = Ui_Dialog()
        dialog.setupUi(dialog)
        dialog.exec()


    def add(self):
        print("aaaa")
        log = Ui_cardTableAddDialog()
        log.setupUi(log)
        log.exec()

    def switchStackView(self):
        print(self.mainWindow.leftListWidget.currentRow())
        currRow = self.mainWindow.leftListWidget.currentRow()
        if currRow == 1:
            self.mainWindow.rightStackedWidget.setCurrentWidget(self.mainWindow.cardTablePage)
        elif currRow == 2:
            self.mainWindow.rightStackedWidget.setCurrentWidget(self.mainWindow.goodsPage)
        elif currRow == 3:
            self.mainWindow.rightStackedWidget.setCurrentWidget(self.mainWindow.salesRecordPage)
        elif currRow == 4:
            self.mainWindow.rightStackedWidget.setCurrentWidget(self.mainWindow.chartsPage)
            self.switchCharts()

    # 图表切换
    def switchCharts(self):
        currIndex = self.mainWindow.chartsTabWidget.tabBar().currentIndex()
        if currIndex == 0:
            c = (Bar().add_xaxis(Faker.days_attrs).add_yaxis(
                "商家A", Faker.days_values).set_global_opts(
                title_opts=opts.TitleOpts(title="Bar-DataZoom（slider-水平）"),
                datazoom_opts=opts.DataZoomOpts(),
            ))
            self.mainWindow.chartWebView1.setHtml(c.render_embed())
        elif currIndex == 1:
            c = (Bar().add_xaxis(Faker.days_attrs).add_yaxis(
                "商家A", Faker.days_values).set_global_opts(
                title_opts=opts.TitleOpts(title="Bar-DataZoom（slider-水平）"),
                datazoom_opts=opts.DataZoomOpts(),
            ))
            self.mainWindow.chartWebView2.setHtml(c.render_embed())
        elif currIndex == 2:
            c = (Bar().add_xaxis(Faker.days_attrs).add_yaxis(
                "商家A", Faker.days_values).set_global_opts(
                title_opts=opts.TitleOpts(title="Bar-DataZoom（slider-水平）"),
                datazoom_opts=opts.DataZoomOpts(),
            ))
            self.mainWindow.chartWebView3.setHtml(c.render_embed())
